package counter;


import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

/**
 * @author 92Yan
 * @create 2019-09-27-19:53
 */
public class CounterFrame extends JFrame {
    private static int id = 0;
    private static int count = 0;
    private static boolean realPathFlag = true;
    private static int width = 640;
    private static int height = 480;
    private static Font font = new Font("微软雅黑", 0, 14);
    private JTextField text_path;
    private JTextField text_suffix;
    private JButton btn_about;
    private JButton btn_count;
    private JButton btn_open;
    private JButton btn_flag;
    private JButton btn_clear;
    private JTextArea text_status;
    private JLabel lab_rowCount;
    private CounterFrame _this = this;
    private File selectedFile;

    private CounterFrame() {
        this.setTitle("代码行数计算工具");
        this.setFont(font);
        this.text_status = new JTextArea();
        this.text_status.setFont(font);
        this.text_status.setEditable(false);
        this.btn_count = new JButton("获取代码行数");
        this.btn_count.setFont(font);
        this.btn_open = new JButton("打开文件夹");
        this.btn_open.setFont(font);
        this.btn_flag = new JButton("改为输出文件名称");
        this.btn_flag.setFont(font);
        this.btn_about = new JButton("关于");
        this.btn_about.setFont(font);
        this.btn_clear = new JButton("重置");
        this.btn_clear.setFont(font);
        this.text_path = new JTextField();
        this.text_path.setFont(font);
        this.text_path.setEditable(false);
        this.text_suffix = new JTextField();
        this.text_suffix.setFont(font);
        this.lab_rowCount = new JLabel("行数：0");
        this.lab_rowCount.setFont(font);
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(3);
        Container contentPane = this.getContentPane();
        JPanel pathPane = new JPanel();
        pathPane.setPreferredSize(new Dimension(width, height / 12));
        JLabel lab_path = new JLabel("路径:", 0);
        JLabel lab_suffix = new JLabel("文件后缀名：", 0);
        lab_suffix.setFont(font);
        lab_path.setFont(font);
        pathPane.setLayout((LayoutManager) null);
        lab_path.setBounds(0, 0, width / 12, height / 12);
        lab_suffix.setBounds(width * 8 / 12, 0, width * 2 / 12, height / 12);
        this.text_path.setBounds(width / 12, 0, width * 7 / 12, height / 12);
        this.text_suffix.setBounds(width * 10 / 12, 0, width * 2 / 12, height / 12);
        this.text_suffix.setText("java");
        pathPane.add(lab_path);
        pathPane.add(this.text_path);
        pathPane.add(lab_suffix);
        pathPane.add(this.text_suffix);
        contentPane.add(pathPane, "North");
        JScrollPane scrollPane = new JScrollPane(this.text_status);
        scrollPane.setPreferredSize(new Dimension(width, height * 5 / 6));
        contentPane.add(scrollPane, "Center");
        JPanel btn_pane = new JPanel();
        btn_pane.add(this.lab_rowCount);
        btn_pane.add(this.btn_open);
        btn_pane.add(this.btn_count);
        btn_pane.add(this.btn_flag);
        btn_pane.add(this.btn_clear);
        btn_pane.add(this.btn_about);
        btn_pane.setPreferredSize(new Dimension(width, height / 12));
        contentPane.add(btn_pane, "South");
        this.btn_open.addActionListener((e) -> {
            JFileChooser chooser = new JFileChooser();
            FileSystemView fsv = FileSystemView.getFileSystemView();
            File homeDirectory = fsv.getHomeDirectory();
            chooser.setCurrentDirectory(homeDirectory);
            chooser.setDialogTitle("请选择要计数的文件");
            chooser.setApproveButtonText("选择");
            chooser.setFileSelectionMode(2);
            int result = chooser.showOpenDialog(this._this);
            if (0 == result) {
                this.selectedFile = chooser.getSelectedFile();
                this.setPath();
            }

        });
        this.btn_count.addActionListener((e) -> {
            try {
                if (this.selectedFile == null) {
                    throw new FilePathNullException("所选文件或文件夹路径不能为空！");
                }

                if (this.text_suffix.getText().length() == 0) {
                    throw new IOException("文件后缀名不能为空!");
                }

                this.CountAllFiles(this.selectedFile);
                this.lab_rowCount.setText("行数：" + count);
                this._this.pack();
            } catch (IOException | FilePathNullException var3) {
                JOptionPane.showMessageDialog(this._this, var3.getMessage(), "错误", 0);
            }

        });
        this.btn_flag.addActionListener((e) -> {
            if (realPathFlag) {
                realPathFlag = false;
                this.btn_flag.setText("改为输出文件全称");
            } else {
                realPathFlag = true;
                this.btn_flag.setText("改为输出文件名称");
            }

        });
        this.btn_about.addActionListener((e) -> {
            String message = "作者：92Yan\n版本：1.2.1\n联系我：1789446861@qq.com\n";
            JOptionPane.showMessageDialog(this._this, message, "关于", -1);
        });
        this.btn_clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CounterFrame.this.text_status.setText("");
                CounterFrame.this.text_path.setText("");
                CounterFrame.this.lab_rowCount.setText("行数：0");
                CounterFrame.count = 0;
                CounterFrame.id = 0;
                CounterFrame.this.selectedFile = null;
            }
        });
        this.setSize(width, height);
        this.setLocationRelativeTo((Component) null);
        this.setVisible(true);
    }

    private void setPath() {
        String s = this.selectedFile.getPath() + ";";
        this.text_path.setText(s);
    }

    private void CountAllFiles(File file) throws IOException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                File[] var3 = files;
                int var4 = files.length;

                for (int var5 = 0; var5 < var4; ++var5) {
                    File f = var3[var5];
                    this.CountAllFiles(f);
                }
            }
        } else {
            String fileName = file.getName();
            if (this.text_suffix.getText().equals(fileName.substring(fileName.lastIndexOf(".") + 1))) {
                int rowCount = this.getRowCount(file);
                count += rowCount;
                ++id;
                String realPath = id + "." + file.getAbsolutePath().substring(this.text_path.getText().length()) + ":" + rowCount + "\n";
                String relativePath = id + "." + file.getName() + ":" + rowCount + "\n";
                if (realPathFlag) {
                    this.text_status.append(realPath);
                } else {
                    this.text_status.append(relativePath);
                }
            }
        }

    }

    private int getRowCount(File file) throws IOException {
        FileReader fileReader = new FileReader(file);
        LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
        lineNumberReader.skip(9223372036854775807L);
        int lineNumber = lineNumberReader.getLineNumber();
        lineNumberReader.close();
        return lineNumber;
    }

    public static void main(String[] args) throws IOException {
        new CounterFrame();
    }
}

class FilePathNullException extends Exception {
    public FilePathNullException() {
    }

    public FilePathNullException(String message) {
        super(message);
    }
}


